package qumaimai.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import qumaimai.dao.AddressDao;
import qumaimai.entity.Address;
import qumaimai.entity.Goods;
import qumaimai.entity.vo.CartVo;
import qumaimai.util.JdbcUtil;

public class AddressDaoImpl implements AddressDao {

	@Override
	public Address getDefault(int userId) {
		Address address = null;
		String sql = "select * from t_user_address where is_delete=0 and is_default=1 and  user_id = "+userId;
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			while(rs.next()){
				int id = rs.getInt("id");
				int userId2 = rs.getInt("user_id");
				String province = rs.getString("province");
				String city = rs.getString("city");
				String district = rs.getString("district");
				String address2 = rs.getString("address");
				Date createTime = rs.getDate("create_time");
				int isDefault = rs.getInt("is_default");
				String remark = rs.getString("remark");
				String receiver = rs.getString("receiver");
				String email = rs.getString("email");
				String phone = rs.getString("phone");
				String part = rs.getString("part");
				Date receiveTime = rs.getDate("receive_time");
				address = new Address(id, userId2, province, city, district, address2, createTime, isDefault, remark, receiver, email, phone, part, receiveTime);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return address;
	}

	@Override
	public List<Address> findAll(int userId) {
		List<Address> list = new ArrayList<Address>();
		String sql = "select * from t_user_address where is_delete=0 and user_id="+userId;
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			while(rs.next()){
				int id = rs.getInt("id");
				int userId2 = rs.getInt("user_id");
				String province = rs.getString("province");
				String city = rs.getString("city");
				String district = rs.getString("district");
				String address2 = rs.getString("address");
				Date createTime = rs.getDate("create_time");
				int isDefault = rs.getInt("is_default");
				String remark = rs.getString("remark");
				String receiver = rs.getString("receiver");
				String email = rs.getString("email");
				String phone = rs.getString("phone");
				String part = rs.getString("part");
				Date receiveTime = rs.getDate("receive_time");
				Address address = new Address(id, userId2, province, city, district, address2, createTime, isDefault, remark, receiver, email, phone, part, receiveTime);
				list.add(address);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public int add(Address address) {
		String sql = "INSERT INTO t_user_address (user_id, address, remark, receiver, email, phone, part, receive_time, province, city, district) VALUES ( "+address.getUserId()+", '"+address.getAddress()+"',  '"+address.getRemark()+"', '"+address.getReceiver()+"', NULL, '"+address.getPhone()+"', NULL, NULL, '"+address.getProvince()+"', '"+address.getCity()+"', '"+address.getDistrict()+"')";
		System.out.println(sql);
		return JdbcUtil.update(sql);
	}

	@Override
	public int edit(Address address) {
		String sql = "UPDATE t_user_address SET  address='"+address.getAddress()+"',   remark='"+address.getRemark()+"', receiver='"+address.getReceiver()+"', email=NULL, phone='"+address.getPhone()+"', part=NULL, receive_time=NULL, province='"+address.getProvince()+"', city='"+address.getCity()+"', district='"+address.getDistrict()+"' WHERE id="+address.getId();
		System.out.println(sql);
		return JdbcUtil.update(sql);
	}

	@Override
	public Address findById(int id) {
		Address address = null;
		String sql = "select * from t_user_address where  is_delete=0 and id = "+id;
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			while(rs.next()){
				int userId2 = rs.getInt("user_id");
				String province = rs.getString("province");
				String city = rs.getString("city");
				String district = rs.getString("district");
				String address2 = rs.getString("address");
				Date createTime = rs.getDate("create_time");
				int isDefault = rs.getInt("is_default");
				String remark = rs.getString("remark");
				String receiver = rs.getString("receiver");
				String email = rs.getString("email");
				String phone = rs.getString("phone");
				String part = rs.getString("part");
				Date receiveTime = rs.getDate("receive_time");
				address = new Address(id, userId2, province, city, district, address2, createTime, isDefault, remark, receiver, email, phone, part, receiveTime);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return address;
	}

	@Override
	public int del(int id) {
		String sql = "update t_user_address set is_delete=1 where id="+id;
		return JdbcUtil.update(sql);
	}

	@Override
	public int setDefault(int id) {
		String sql = "update t_user_address set is_default=1 where id="+id;
		return JdbcUtil.update(sql);
	}

	@Override
	public int clearDefault(int userId) {
		String sql = "update t_user_address set is_default=0 where user_id="+userId;
		return JdbcUtil.update(sql);
	}

}
